package com.yaozheng.vocationaltraining.fragment.userinfo;

import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.fragment.BaseFragment;
import com.yaozheng.vocationaltraining.iview.userinfo.ILogoutView;
import com.yaozheng.vocationaltraining.iview.userinfo.IUpdatePasswordView;
import com.yaozheng.vocationaltraining.service.impl.userinfo.LogoutServiceImpl;
import com.yaozheng.vocationaltraining.service.impl.userinfo.UpdatePasswordServiceImpl;
import com.yaozheng.vocationaltraining.service.userinfo.LogoutService;
import com.yaozheng.vocationaltraining.service.userinfo.UpdatePasswordService;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

/**
 * 修改密码
 */
@EFragment(R.layout.fragment_update_password)
public class UpdatePasswordFragment extends BaseFragment implements IUpdatePasswordView {
    @ViewById
    EditText passwordEdit, newPasswordEdit, newPasswordEdit2;

    @ViewById
    TextView headTitleText, headRightText;
    @Bean(UpdatePasswordServiceImpl.class)
    UpdatePasswordService updatePasswordService;

    @AfterViews
    public void initView() {
        updatePasswordService.init(this);
        headTitleText.setText("修改密码");
        headRightText.setText("提交");
        headRightText.setVisibility(View.VISIBLE);
        headRightText.setTextColor(getResources().getColor(R.color.leran_list_type_item_selected_font_color));
    }

    /**
     * 保存点击
     */
    @Click(R.id.headRightText)
    public void headRightClick() {
        String password = passwordEdit.getText().toString();
        String newPassword = newPasswordEdit.getText().toString();
        String newPassword2 = newPasswordEdit2.getText().toString();
        if (password == null || "".equals(password)) {
            alertMessage("请输入原来密码");
        } else if (newPassword == null || "".equals(newPassword)) {
            alertMessage("请输入新密码");
        } else if (newPassword2 == null || "".equals(newPassword2)) {
            alertMessage("请再次输入新密码");
        } else {

            if (!newPassword2.equals(newPassword)) {
                alertMessage("两次密码不一致");
            } else if (password.equals(newPassword)) {
                alertMessage("新密码不能与原来密码一样");
            } else {
                showOperatingProgressDialog("修改密码中...");
                updatePasswordService.updatePassword(password, newPassword, newPassword2);
            }
        }

    }

    @UiThread
    @Override
    public void updatePasswordSuccess(JSONObject jsonObject) {
        cancelOperatingProgressDialog();
        boolean result = TypeUtils.getJsonBoolean(TypeUtils.getJsonObject(jsonObject, "data"), "result", false);
        if (result) {
            alertMessage("修改成功");
            getBaseActivity().signOutOperating();
        } else {
            alertMessage("修改失败");
        }
    }

    @UiThread
    @Override
    public void updatePasswordError(String error) {
        cancelOperatingProgressDialog();
        alertMessage(error);
    }


}
